﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Traceability;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Traceability
{
    public class GetTraceabilityHandler : IRequestHandler<GetTraceabilityCommand, ApiPaging<TraceabilityDTO>>
    {
        private readonly IBase<TraceabilityModel> traceabilityRepo;
        private readonly IBase<GoodsModel> goodsRepo;

        public GetTraceabilityHandler(IBase<TraceabilityModel> traceabilityRepo,IBase<GoodsModel> goodsRepo)
        {
            this.traceabilityRepo = traceabilityRepo;
            this.goodsRepo = goodsRepo;
        }

        public Task<ApiPaging<TraceabilityDTO>> Handle(GetTraceabilityCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<TraceabilityDTO> result =new ApiPaging<TraceabilityDTO>();
            var traceability = traceabilityRepo.GetAll().Where(x => !x.IsDeleted);
            var goods = goodsRepo.GetAll();
       
            //生产批次(整个系统唯一)
            if (!string.IsNullOrEmpty(request.Batch))
            {
                traceability = traceability.Where(x=>x.Batch.Contains(request.Batch));
            }
            // 保质期(天)
            if (request.Shelf > 0)
            {
                traceability = traceability.Where(x => x.Shelf == request.Shelf);
            }
            // 生产许可证(整系统唯一)
            if (!string.IsNullOrEmpty(request.License))
            {
                traceability = traceability.Where(x => x.License.Contains(request.License));
            }
            // 生产企业
            if (!string.IsNullOrEmpty(request.Enterprise))
            {
                traceability = traceability.Where(x => x.Enterprise.Contains(request.Enterprise));
            }
            // 创建人
            if (!string.IsNullOrEmpty(request.CreatreName))
            {
                traceability = traceability.Where(x => x.CreatreName.Contains(request.CreatreName));
            }
            // 创建时间
            if (request.CreateTime!=null)
            {
                traceability = traceability.Where(x =>x.CreateTime==request.CreateTime);
            }
            // 修改人
            if (!string.IsNullOrEmpty(request.UpdateName))
            {
                traceability = traceability.Where(x => x.UpdateName.Contains(request.UpdateName));
            }

            var linq = from a in traceability
                       join b in goods
                       on a.GoodsId equals b.GoodsId
                       select new TraceabilityDTO
                       {
                           Batch=a.Batch,
                           Completeness=a.Completeness,
                           Cover=a.Cover,
                           CreateTime=a.CreateTime,
                           CreatreName=a.CreatreName,
                           Enterprise=a.Enterprise,
                           GoodsId=a.GoodsId,
                           GoodsName=b.GoodsName,
                           License=a.License,
                           Shelf=a.Shelf,
                           TraceabilityId=a.TraceabilityId,
                           UpdateName=a.UpdateName,

                       };
            //商品Id
            if (!string.IsNullOrEmpty(request.GoodsName))
            {
                goods = goods.Where(x => x.GoodsName.Contains(request.GoodsName));
            }
            result.Code = ApiEnum.OK;
            result.Data =linq.ToList();
            result.Msg = "溯源基本信息显示查询成功";
            return Task.FromResult(result);

        }
    }
}
